package com.carepad.action.weekdata;

import java.util.Date;
import java.util.List;

import com.carepad.entity.Records;
import com.carepad.entity.mapper.ChartData;
import com.carepad.entity.mapper.SelectClient;
import com.carepad.util.DateConverterEnJa;

public class WeekDataViewAction extends AbstractWeekDataAction {

	private static final long serialVersionUID = 1L;

	private List<Records> weekDataRecords;
	private List<ChartData> chartDataRecords;
	private List<String> weekDaysJa;
	private List<Date> weekDaysEn;

	private String weekStartDate;
	private String weekEndDate;
	private String btnValue;
	private int currentWeekDay;
	

	private String patientClientUID;

	@SuppressWarnings("deprecation")
	public String execute() {
		try {
			SelectClient patient = null;
			Date startDate = null;
			Date lastDate = null;			

			String lastDateString = null;
			String startDateString = null;
			Date today = new Date();
			int dayofweek = today.getDay();
			
			Date sessionDate = getSessionImplementationDate().getEnDate();
			int dayofsessionweek = sessionDate.getDay();
			
			if (btnValue == null) {
				btnValue = "first";
			}
			
			if (!btnValue.equals("getweek")) {
			
				if(weekStartDate != null && weekEndDate != null){
					startDate = DateConverterEnJa.JaDatetoEnDate(weekStartDate);
					lastDate = DateConverterEnJa.JaDatetoEnDate(weekEndDate);
				}
				
				if(btnValue.equals("prev")){

					startDate = new Date(startDate.getYear(), startDate.getMonth(),
							startDate.getDate() - 7);				
					lastDate = new Date(lastDate.getYear(), lastDate.getMonth(),
							lastDate.getDate() - 7);
					
					dayofweek = -1;
					
				} else if(btnValue.equals("next")){
					
					startDate = new Date(startDate.getYear(), startDate.getMonth(),
							startDate.getDate() + 7);				
					lastDate = new Date(lastDate.getYear(), lastDate.getMonth(),
							lastDate.getDate() + 7);
					
					if(!lastDate.after(today)) {
						dayofweek = -1;
					}
					
				} else if(btnValue.equals("this")){
					
					startDate = new Date(today.getYear(), today.getMonth(),
							today.getDate() - dayofweek + 1);						
					lastDate = new Date(today.getYear(), today.getMonth(),
							today.getDate() - dayofweek + 7);
								
				} else {

					if(dayofsessionweek!=0){
						startDate = new Date(sessionDate.getYear(), sessionDate.getMonth(),	
								sessionDate.getDate() - dayofsessionweek + 1);
						lastDate = new Date(sessionDate.getYear(), sessionDate.getMonth(), 
								sessionDate.getDate() - dayofsessionweek + 7);						
					} else {
						startDate = new Date(sessionDate.getYear(), sessionDate.getMonth(),	
								sessionDate.getDate() - dayofsessionweek - 6);
						lastDate = new Date(sessionDate.getYear(), sessionDate.getMonth(), 
								sessionDate.getDate() - dayofsessionweek );					
					}
					if(!lastDate.after(today)) {
						dayofweek = -1;
					}
				}
				
				setWeekStartDate(DateConverterEnJa.EnDatetoJa(startDate));
				setWeekEndDate(DateConverterEnJa.EnDatetoJa(lastDate));
				
			} else {
				
				startDate = DateConverterEnJa.JaDatetoEnDate(weekStartDate);
				lastDate = DateConverterEnJa.JaDatetoEnDate(weekEndDate);
				
				if(!lastDate.after(today)) {
					dayofweek = -1;
				}
				
			}
			
			setCurrentWeekDay(dayofweek);

			if (patientClientUID != null) {

				patient = getPatientByUid(patientClientUID);
				addSessionVariable(patient);
				setCookie("CLIENTUID",patientClientUID);
			}
			
			setWeekDaysJa(getJaWeekDaysList(startDate, lastDate));
			setWeekDaysEn(getEnWeekDaysList(startDate, lastDate));

			startDateString = DateConverterEnJa.JaDatetoEnString(weekStartDate);
			lastDateString = DateConverterEnJa.JaDatetoEnString(weekEndDate);

			weekDataRecords = recordsService.findRecordListByHcsId(
					getSessionPatient().getHcsID(), startDateString, lastDateString);
			chartDataRecords = chartDataService.findChartDataListByHcsId(
					getSessionPatient().getHcsID(), startDateString, lastDateString);

			return SUCCESS;
			
		} catch (Exception e) {
			e.printStackTrace();

			return ERROR;
		}
	}

	public List<Records> getWeekDataRecords() {
		return weekDataRecords;
	}

	public void setWeekDataRecords(List<Records> weekDataRecords) {
		this.weekDataRecords = weekDataRecords;
	}

	public List<ChartData> getChartDataRecords() {
		return chartDataRecords;
	}

	public void setChartDataRecords(List<ChartData> chartDataRecords) {
		this.chartDataRecords = chartDataRecords;
	}

	public String getPatientClientUID() {
		return patientClientUID;
	}

	public void setPatientClientUID(String patientClientUID) {
		this.patientClientUID = patientClientUID;
	}

	public String getWeekStartDate() {
		return weekStartDate;
	}

	public void setWeekStartDate(String weekStartDate) {
		this.weekStartDate = weekStartDate;
	}

	public String getWeekEndDate() {
		return weekEndDate;
	}

	public void setWeekEndDate(String weekEndDate) {
		this.weekEndDate = weekEndDate;
	}
	
	public String getBtnValue() {
		return btnValue;
	}

	public void setBtnValue(String btnValue) {
		this.btnValue = btnValue;
	}

	public int getCurrentWeekDay() {
		return currentWeekDay;
	}

	public void setCurrentWeekDay(int currentWeekDay) {
		this.currentWeekDay = currentWeekDay;
	}

	public List<String> getWeekDaysJa() {
		return weekDaysJa;
	}

	public void setWeekDaysJa(List<String> weekDaysJa) {
		this.weekDaysJa = weekDaysJa;
	}

	public List<Date> getWeekDaysEn() {
		return weekDaysEn;
	}

	public void setWeekDaysEn(List<Date> weekDaysEn) {
		this.weekDaysEn = weekDaysEn;
	}
}
